package com.elitel.frame.base.controller;

import com.elitel.common.auth.AuthProperties;
import com.elitel.common.pagelist.PageHelper;
import com.elitel.common.core.log.annotation.Log;
import com.elitel.common.core.log.enums.BusinessType;
import com.elitel.frame.main.entity.SysWhitelist;
import com.elitel.frame.base.service.SysWhitelistService;
import com.elitel.frame.main.entity.vo.JsonResult;
import com.elitel.frame.main.entity.vo.PageResponseVo;
import com.elitel.common.utils.SysUserUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * cas白名单(SysWhitelist)表控制层
 *
 * @author makejava
 * @since 2024-01-02 18:10:07
 */
@RestController
@RequestMapping("sysWhitelist")
public class SysWhitelistController {
    /**
     * 服务对象
     */
    @Resource
    private SysWhitelistService sysWhitelistService;
    @Resource
    private AuthProperties authProperties;

    /**
     * 新增数据
     *
     * @param sysWhitelist 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public JsonResult add(SysWhitelist sysWhitelist) {
        JsonResult jsonResult = new JsonResult();
        this.sysWhitelistService.insert(sysWhitelist);
        jsonResult.setSuccess(true);
        return jsonResult;
    }


    @GetMapping("list")
    @Log(title = "查询白名单", businessType = BusinessType.SELECT)
    public JsonResult list(SysWhitelist sysWhitelist) {
        JsonResult jsonResult = new JsonResult();
        PageHelper.startPage(sysWhitelist.getPageNum(), sysWhitelist.getPageSize());
        PageInfo<SysWhitelist> pageList = new PageInfo<>(this.sysWhitelistService.queryList(sysWhitelist));
        PageResponseVo<SysWhitelist> res = new PageResponseVo<>();
        res.setRows(pageList.getList());
        res.setTotal(((Long) pageList.getTotal()).intValue());
        jsonResult.setSuccess(true);
        jsonResult.setData(res);
        return jsonResult;
    }

    @PostMapping("save")
    public JsonResult save(@RequestBody SysWhitelist sysWhitelist) {
        JsonResult jsonResult = new JsonResult();
        sysWhitelist.setCreatedUser(SysUserUtils.getLoginAccount());
        sysWhitelist.setVerifyFiledDesc(authProperties.getVerifyFieldDesc());
        sysWhitelistService.save(sysWhitelist);
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    @PostMapping("updateStatus")
    public JsonResult updateStatus(@RequestBody SysWhitelist sysWhitelist) {
        JsonResult jsonResult = new JsonResult();
        sysWhitelistService.updateStatus(sysWhitelist);
        jsonResult.setSuccess(true);
        return jsonResult;
    }



    @PostMapping("delete")
    public JsonResult delete(@RequestBody SysWhitelist sysWhitelist) {
        JsonResult jsonResult = new JsonResult();
        sysWhitelistService.deleteById(sysWhitelist.getId());
        jsonResult.setSuccess(true);
        return jsonResult;
    }



    @GetMapping("getVerifyFieldDesc")
    public JsonResult getVerifyFieldDesc() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(authProperties.getVerifyFieldDesc());
        jsonResult.setSuccess(true);
        return jsonResult;
    }

}

